<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <div class="input-search">
        <el-input
          clearable
          @clear="getGoods"
          placeholder="请输入内容"
          class="input-with-select"
          v-model="info.query"
        >
          <el-button slot="append" icon="el-icon-search" @click="getGoods"></el-button>
        </el-input>
        <!-- 添加按钮区域 -->
        <el-button type="primary" @click="goAddPages">添加商品</el-button>
      </div>
      <!-- table区域 -->
      <el-table :data="tableListData" style="width: 100%" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="600"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="110"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="90"></el-table-column>
        <el-table-column prop="add_time" label="创建时间">
          <template slot-scope="scope">{{scope.row.add_time | dataFormat}}</template>
        </el-table-column>
        <el-table-column prop="address" label="操作">
          <template slot-scope="scope">
            <!-- 编辑 -->
            <el-button size="mini" type="primary" icon="el-icon-edit"></el-button>
            <!-- 删除 -->
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="listDelete(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="info.pagenum"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="info.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      info: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      tableListData: [],
      total: 0
    }
  },
  created() {
    this.getGoods()
  },
  methods: {
    async getGoods() {
      const { data: res } = await this.$http.get('goods', { params: this.info })
      console.log(res)
      if (res.meta.status !== 200) return this.$msg('获取商品列表失败')
      this.tableListData = res.data.goods
      this.total = res.data.total
    },
    //当页号发生改变时，更改pagesize，重新请求
    handleSizeChange(newSize) {
      this.info.pagesize = newSize
      this.getGoods()
    },
    //当页码发生改变时，更改pagesize，重新请求
    handleCurrentChange(newPage) {
      this.info.pagenum = newPage
      this.getGoods()
    },
    // 删除
    async listDelete(id) {
      const confirmDelete = await this.$confirm(
        '此操作将永久删除该文件, 是否继续?',
        '提示',
        {
          cancelButtonClass: 'btn-custom-cancel',
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      if (confirmDelete !== 'confirm') return this.$msg('已取消删除', 'info')
      const { data: res } = await this.$http.delete('goods/' + id)
      console.log(res)
      if (res.meta.status !== 200) return this.$msg('删除失败')
      this.$msg('删除成功', 'success')
      this.getGoods()
    },
    // 去添加商品页面
    goAddPages() {
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style lang="less" scoped>
.input-with-select {
  width: 340px;
  // padding: 0 px 0 0px;
  margin-right: 15px;
}
.input-search {
  margin-bottom: 15px;
}
</style>